package com.team.olympic.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.team.olympic.dto.UserVoteDto;
import com.team.olympic.mapper.ItemMapper;
import com.team.olympic.mapper.UserMapper;
import com.team.olympic.mapper.MatchMapper;
import com.team.olympic.pojo.Item;
import com.team.olympic.pojo.User;
import com.team.olympic.util.MD5Util;
import com.team.olympic.util.SnowFlake;
import com.team.olympic.vo.ItemVo;
import com.team.olympic.mapper.ItemVoMapper;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemVoMapper ItemVoMapper;
    @Autowired
    private MatchMapper matchMapper;
    @Autowired
    private SnowFlake snowFlake;

    public List<User> findAllUsers() {
        return userMapper.findAllUsers();
    }

    public User login(User user) {
        if (user == null || user.getPhone() == null || user.getPassword() == null) {
            return null;
        }
        User loginUser = userMapper.findUserByPhone(user.getPhone());
        if (loginUser == null) {
            return null; // 没有这个用户
        }
        if (loginUser.getStatus() == 1) {
            return null; // 用户被封禁
        }
        if (loginUser != null && MD5Util.checkPassword(user.getPassword(), loginUser.getPassword())) {
            return loginUser;
        }
        return null;
    }

    public User register(User user) {
        if (user == null || user.getPhone() == null || user.getPassword() == null) {
            return null;
        }
        if (userMapper.findUserByPhone(user.getPhone()) != null) {
            return null; // 已经被注册了
        }
        user.setId(snowFlake.nextId());
        user.setPassword(MD5Util.getMD5String(user.getPassword()));
        user.setName(user.getPhone()); // 默认昵称为手机号
        user.setScore(0L);
        user.setStatus(0L);
        userMapper.insertUser(user);
        return userMapper.findUserById(user.getId());
    }

    @Transactional
    public boolean userVote(UserVoteDto userVoteDto) {
        if (userVoteDto == null || userVoteDto.getUserId() == null || userVoteDto.getMatchId() == null
        || userVoteDto.getWin() == null) {
            return false;
        }
        if (userMapper.findUserById(userVoteDto.getUserId()) == null) {
            return false; // 没有这个用户
        }
        if (matchMapper.findMatchByMatchId(userVoteDto.getMatchId()) == null) {
            return false; // 没有这个比赛
        }
        if (userMapper.findUserById(userVoteDto.getUserId()).getStatus() == 1) {
            return false; // 用户被封禁
        }
        userMapper.insertUserVote(userVoteDto);
        if (userVoteDto.getWin()) { // 赢了要加分
            userMapper.increUserScoreById(userVoteDto.getUserId());
        }
        return true;
    }

    public void deleteUserById(Long userId) {
        userMapper.deleteUserById(userId);
    }

    public void declineUserScoreById(Long userId, Long score) {
        userMapper.declineUserScoreById(userId, score);
    }

    public User findUserById(Long userId) {
        return userMapper.findUserById(userId);
    }

    public boolean buyItem(Long userId, Long itemId) {
        Item item = itemMapper.findItemById(itemId);
        if (item == null || item.getCost() == null || item.getStore() == null) {
            return false;
        }
        if (userMapper.findUserById(userId) == null) {
            return false; // 没有这个用户
        }
        if (itemMapper.findItemById(itemId) == null) {
            return false; // 没有这个商品
        }
        if (userMapper.findUserById(userId).getStatus() == 1) {
            return false; // 用户被封禁
        }
        if (item.getStore() <= 0) {
            return false; // 库存不能小于0
        }
        User user = userMapper.findUserById(userId);
        if (user == null || user.getScore() == null || user.getScore() < item.getCost()) {
            return false; // 买不起
        }
        userMapper.declineUserScoreById(userId, item.getCost()); // 需要减去分数
        itemMapper.declineStoreById(itemId); // 需要减去库存
        itemMapper.insertItemUser(userId, itemId);
        return true;
    }

    public void blockUser(Long userId) {
        userMapper.blockUserById(userId);
    }

    public void freeUser(Long userId) {
        userMapper.freeUserById(userId);
    }

    public boolean updateUser(User user) {
        if (userMapper.findUserById(user.getId()) == null) {
            return false; // 没有这个用户
        }
        userMapper.updateUser(user);
        return true;
    }

    public List<ItemVo> findMyItems(Long userId) {
        return ItemVoMapper.findItemsByUserId(userId);
    }
}
